import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards, UsePipes } from '@nestjs/common';
import { DeptService } from './dept.service';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import {
  CreateDeptDto,
  UpdateDeptDto,
  createDeptSchema,
  updateDeptSchema,
  paramSchema,
  queryDeptSchema,
  QueryDeptDto,
} from './dto/dept.schema';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ApiTags, ApiBody, ApiOperation } from '@nestjs/swagger';
@ApiTags('部门模块')
@Controller('depts')
@UseGuards(JwtAuthGuard)
export class DeptController {
  constructor(private readonly deptService: DeptService) { }

  @Post()
  @ApiOperation({ summary: '新增部门' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        parentId: {
          type: 'string',
          description: '部门id',
          example: "100"
        },
      }
    }
  })
  @UsePipes(new ZodValidationPipe(createDeptSchema))
  create(@Body() createDeptDto: CreateDeptDto) {
    return this.deptService.create(createDeptDto);
  }

  @Get()
  @ApiOperation({ summary: '查询部门列表' })
  findAll(@Query(new ZodValidationPipe(queryDeptSchema)) query?: QueryDeptDto) {
    return this.deptService.findAll(query);
  }

  @Get(':id')
  @ApiOperation({ summary: '根据id查询部门' })
  @UsePipes(new ZodValidationPipe(paramSchema))
  findOne(@Param() params: { id: string }) {
    return this.deptService.findOne(+params.id);
  }

  @Put(':id')
  @ApiOperation({ summary: '根据id更新部门' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        deptId: {
          type: 'string',
          description: '部门id',
          example: "100"
        },
      }
    }
  })
  @UsePipes(new ZodValidationPipe(paramSchema))
  update(
    @Param() params: { id: string },
    @Body(new ZodValidationPipe(updateDeptSchema)) updateDeptDto: UpdateDeptDto,
  ) {
    return this.deptService.update(+params.id, updateDeptDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '根据id删除部门' })
  @UsePipes(new ZodValidationPipe(paramSchema))
  remove(@Param() params: { id: string }) {
    return this.deptService.remove(+params.id);
  }
}